#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
WMS Simple 简易客户端
使用web浏览器作为界面，避免tkinter依赖问题
"""

import os
import sys
import json
import webbrowser
from pathlib import Path
import time

# 添加项目根目录到Python路径
current_dir = Path(__file__).parent
sys.path.insert(0, str(current_dir))

# 设置工作目录
os.chdir(current_dir)

def load_config():
    """加载配置"""
    config_file = Path("client_config.json")
    default_config = {
        "api_host": "127.0.0.1",
        "api_port": 58501,
        "connection_timeout": 5,
        "auto_refresh_interval": 30
    }
    
    try:
        if config_file.exists():
            with open(config_file, 'r', encoding='utf-8') as f:
                config_data = json.load(f)
                config = {**default_config, **config_data}
        else:
            config = default_config.copy()
            with open(config_file, 'w', encoding='utf-8') as f:
                json.dump(config, f, indent=2, ensure_ascii=False)
    except Exception as e:
        print(f"加载配置失败: {e}")
        config = default_config.copy()
    
    return config

def main():
    """主函数"""
    print("=" * 40)
    print("WMS Simple 简易客户端")
    print("=" * 40)
    
    # 加载配置
    config = load_config()
    api_url = f"http://{config['api_host']}:{config['api_port']}"
    
    print(f"API服务器地址: {api_url}")
    print("正在打开浏览器...")
    
    # 尝试在浏览器中打开API服务器地址
    try:
        webbrowser.open(api_url)
        print("浏览器已打开，请在浏览器中操作")
        print("提示: 如果API服务器支持Swagger UI，可以访问 {}/docs 进行API测试".format(api_url))
    except Exception as e:
        print(f"打开浏览器失败: {e}")
    
    print("\n按Enter键退出...")
    input()

if __name__ == "__main__":
    main() 